import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;





public class Main2 {
    private static MyStack ms2 = new MyStack();//运算栈


    /**
     * 对逆波兰表达式进行求值
     */
    public static int jisuan(String ls) {
    	if(ls.length()==0||ls.isEmpty()){
    		return 0;
    	}
    	int a = 0,b = 0;
        for (int i = 0;i<ls.length();++i) {
        	String s = ls.charAt(i)+"";
        	//如果为数字则入栈
            if (s.matches("d+")) {
            	if(ms2.size()<16){
            		ms2.push(s);
            	}else{
            		return -2;
            	}
            } else {//如果为运算符号则出站运算，在把运算结果入栈
                if (s.equals("+")) {
                	if(ms2.size()>=2){
	                    b = Integer.parseInt(ms2.pop());
	                    a = Integer.parseInt(ms2.pop());
	                    a = a + b;	
	                //    ms2.push(a+"");
                	}else{
                		return -1;
                	}
                }else if (s.equals("*")) {
                	if(ms2.size()>=2){
	                    b = Integer.parseInt(ms2.pop());
	                    a = Integer.parseInt(ms2.pop());
	                    a = a * b;
	                 //   ms2.push(a+"");
                	}else{
                		return -1;
                	}
                } else if (s.equals("^")) {
                    a = Integer.parseInt(ms2.pop());
                    a = a + 1;
                   // ms2.push(a+"");
                }
                ms2.push("" + a);
            }
        }
        return Integer.parseInt(ms2.pop());
    }


    public static void main(String[] args) {
        System.out.println(jisuan("5 6 8 + -"));
    }

}





class MyStack {
    private List<String> l;
    private int size;
    public String top;

    public MyStack() {
        l = new ArrayList<String>();
        size = 0;
        top = null;
    }

    public int size() {
        return size;
    }

    public void push(String s) {
        l.add(s);
        top = s;
        size++;
    }

    public String pop() {
        String s = l.get(size - 1);
        l.remove(size - 1);
        size--;
        top = size == 0 ? null : l.get(size - 1);
        return s;
    }
}

